//
//  ByCellStoreConsume.m
//  mijifen_ios
//
//  Created by mc on 2017/11/7.
//  Copyright © 2017年 itboye. All rights reserved.
//

#import "ByCellStoreConsume.h"

@interface ByCellStoreConsume()<UITextFieldDelegate>
@end

@implementation ByCellStoreConsume
{
    UIView      *_bgView;
    NSString    *_placeStr;
    NSString    *_buttonStr;
}

+ (instancetype)cellWithTableView:(UITableView*)tableView andPlaceStr:(NSString *)placeStr andDeduct:(NSString *)buttonStr{
    
    static NSString *cellIdentifier = @"ByCellStoreConsume";
    ByCellStoreConsume *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[self alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier andPlaceStr:placeStr andDeduct:buttonStr];
    }
    return cell;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier andPlaceStr:(NSString *)placeStr andDeduct:(NSString *)buttonStr{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _placeStr = placeStr;
        _buttonStr = buttonStr;
        self.selectionStyle = UITableViewCellSeparatorStyleNone;
        self.backgroundColor = [UIColor clearColor];
        [self createViews];
    }
    return self;
}

- (void)createViews{
    _bgView = [[UIView alloc] init];
    [self addSubview:_bgView];
    [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(10));
        make.top.equalTo(@(0));
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH - 20, 50));
    }];
    _bgView.backgroundColor = [UIColor whiteColor];
    _bgView.layer.cornerRadius = 4.0;
    
    CGFloat width = [@"抵扣" getSizeOfString:16].width + 60;
    _deductButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_bgView addSubview:_deductButton];
    if (_buttonStr.length != 0) {
        [_deductButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(@(0));
            make.top.equalTo(@(0));
            make.size.mas_equalTo(CGSizeMake(width, 50));
        }];
    }else{
        [_deductButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(@(0));
            make.top.equalTo(@(0));
            make.size.mas_equalTo(CGSizeMake(0, 0));
        }];

    }
    [_deductButton setTitle:@"抵扣" forState:UIControlStateNormal];
    [_deductButton setTitleColor:BY_TEXT_COLOR forState:UIControlStateNormal];
    [_deductButton setBackgroundColor:BY_THEME_COLOR];
    _deductButton.titleLabel.font = FONT(16);
    [_deductButton addTarget:self action:@selector(deductAction:) forControlEvents:UIControlEventTouchUpInside];
    
    _textField = [[UITextField alloc] init];
    [_bgView addSubview:_textField];
    [_textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(10));
        make.right.equalTo(_deductButton.mas_left).offset(-10);
        make.top.equalTo(@(10));
        make.height.equalTo(@(30));
    }];
    _textField.placeholder = _placeStr;
    _textField.font = FONT(16);
    _textField.delegate = self;
//    if (_buttonStr.length != 0) {
//        _textField.textAlignment = NSTextAlignmentRight;
//    }else{
        _textField.textAlignment = NSTextAlignmentCenter;

//    }
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    if (self.editBlock) {
        self.editBlock(textField.text);
    }
}

#pragma mark - 抵扣
- (void)deductAction:(UIButton *)button{
    // 抵扣
    if ([button.titleLabel.text isEqualToString:@"抵扣"]) {
        if (_textField.text.length == 0) {
            [ByCoreToolAlert showOnlyText:@"请输入积分数量"];
            return;
        }
        [button setTitle:@"撤销" forState:UIControlStateNormal];
    }else{
        [button setTitle:@"抵扣" forState:UIControlStateNormal];
        _textField.text = @"";
    }
    if (self.deductBlock) {
        self.deductBlock(_textField.text);
    }
}

@end
